//
//  CarDetailViewController.m
//  一键车讯
//
//  Created by 江玉元 on 14/10/30.
//  Copyright (c) 2014年 lanou.3g.com. All rights reserved.
//

#import "CarDetailViewController.h"
#import "BrandDetail.h"

@interface CarDetailViewController ()

@property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, retain)UIWebView *webView;

@end

@implementation CarDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    
//    self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y - 64, self.view.frame.size.width, self.view.frame.size.height);
    
    
    // Do any additional setup after loading the view.
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y - 92, self.view.frame.size.width, self.view.frame.size.height + 92)];
    [self.view addSubview:_scrollView];
    
    _scrollView.bounces = NO;
    
    _scrollView.showsVerticalScrollIndicator = NO;
    
    _scrollView.delegate = self;
    
    
    // WebView 的使用

    NSMutableString *str = [NSMutableString stringWithFormat:@"http://price.pcauto.com.cn/sg%@/", self.brandDetail.carId];
    
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height + 46)];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:str] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
    [self.webView loadRequest:request];
    
    self.webView.scrollView.bounces = NO;
    self.webView.scrollView.showsVerticalScrollIndicator = NO;
    [self.scrollView addSubview:self.webView];
    [_webView release];
    [_scrollView release];
    
    // 本地存储，将名字传到最近访问的本地数组中
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *document = [path lastObject];
    NSString *strPath = [document stringByAppendingString:@"/array.xml"];
    NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:strPath];
    
    if (array.count == 0) {
        
        NSMutableArray *arr1 = [NSMutableArray array];
        [arr1 writeToFile:strPath atomically:YES];
    }
    
    [array addObject:self.brandDetail.name];
    
    NSArray *reversedArray = [[array reverseObjectEnumerator] allObjects];
    
    NSMutableArray *array1 = [NSMutableArray arrayWithArray:reversedArray];
    
    //[array addObject:self.brandDetail.carId];
    NSLog(@"-------%@", array);
 
    
    while (array1.count > 4) {
        [array1 removeObjectAtIndex:4];
    }
    
    [array1 writeToFile:strPath atomically:YES];
    
    
//    NSLog(@"%@", array);
    
}

- (void)dealloc
{
    [_scrollView release];
    [_webView release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
